<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>时间轴实现</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f9f9f9;
            margin: 0;
            box-sizing: border-box;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        .timeline-wrapper {
            margin: 8px 20px;
            position: relative;
            overflow-x: auto;
        }

        .timeline-wrapper::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            height: 24px;
            background-color: #f2f2f2;
            transform: translateY(-50%);
            z-index: 1;
            transition: width 0.3s ease;
        }

        .timeline {
            display: flex;
            justify-content: space-between;
            position: relative;
            z-index: 2;
            flex: 0 0 auto;
            width: fit-content;
        }

        .timeline-item {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-grow: 1;
            min-width: 160px; /* 避免过窄导致内容溢出 */
        }

        .timeline-date {
            color: white;
            font-weight: 600;
            background-color: #3968E2;
            min-width: 160px;
            height: 24px;
            text-align: center;
            white-space: nowrap;
            clip-path: polygon(0 0, 85% 0, 100% 100%, 15% 100%);
            background-size: 20px 20px;
        }

        .timeline-event {
            position: absolute;
            word-break: break-word;
            max-width: 240px;
        }

        .timeline-event.is-top {
            bottom: calc(50% + 20px);
            width: auto;
            min-width: 132px;
            display: flex;
            flex-direction: row;
        }

        .timeline-event.is-bottom {
            top: calc(50% + 20px);
            display: flex;
            flex-direction: row;
            min-width: 132px;
            width: auto;
        }

        .timeline-vtc-line-middle {
            width: 19px;
            display: flex;
            flex-direction: column;
            align-content: center;
            align-items: center;
            justify-content: center;
        }

        .timeline-vtc-line-circle {
            width: 6px;
            height: 7px;
            background: #3968e2;
            border-radius: 5px;
        }

        .timeline-vtc-line {
            height: 100%;
            border: 1px solid #3968e2;
        }

        .timeline-vtc-li-base {
            display: flex;
            font-family: PingFangSC-Regular, Arial, sans-serif;
            font-size: 14px;
            font-weight: 400;
            width: calc(100% - 31px);
            height: 100%;
            align-items: center;
        }

        .ebcoms-list-ellipsis {
            color: #3968E2;
            line-height: 1.5;
            cursor: pointer;
            user-select: none;

            word-break: break-word;
            overflow-wrap: break-word;
            white-space: normal;

            width: 100%;
            max-width: 240px;
        }


        .ebcoms-list-ellipsis:hover {
            text-decoration: none;
        }

        .timeline-header {
            padding: 0 14px;
            display: flex;
            align-items: center;
        }

        .timeline-header::before {
            content: '';
            display: inline-block;
            background-color: #105EE9;
            width: 3px;
            height: 16px;
            margin-right: 12px;
            border-radius: 2px;
        }

        .ebcom-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .timeline-view {
            display: flex;
            flex-direction: column;
            width: 100%;
        }

    </style>
</head>
<body>

<h1>我的时间轴</h1>
<div class="timeline-view">
    <div class="timeline-header">
        <div class="ebcom-title">时间轴</div>
    </div>
    <div id="timeline-container" class="timeline-wrapper"></div>
</div>

<script>
    function initTimeline({ containerId, data = [], onItemClick }) {
        const container = document.getElementById(containerId);
        if (!container) return;

        const timelineEl = document.createElement('div');
        timelineEl.classList.add('timeline');

        data.forEach((item, index) => {
            // 空时间或空内容不渲染
            if (!item.date || !item.content) return;

            const timelineItem = document.createElement('div');
            timelineItem.classList.add('timeline-item');
            timelineItem.id = item.id;
            const position = (index % 2 === 0) ? 'top' : 'bottom';
            const positionClass = `is-${position}`;

            const circleLine = document.createElement('div');
            circleLine.className = 'timeline-vtc-line-middle';
            circleLine.innerHTML = (position === 'top')
                ? `<div class="timeline-vtc-line-circle"></div><div class="timeline-vtc-line"></div>`
                : `<div class="timeline-vtc-line"></div><div class="timeline-vtc-line-circle"></div>`;

            const baseLine = document.createElement('div');
            baseLine.className = 'timeline-vtc-li-base';

            const contentDiv = document.createElement('div');
            contentDiv.className = 'ebcoms-list-ellipsis';
            contentDiv.title = item.content;
            contentDiv.textContent = item.content;

            contentDiv.onclick = function (e) {
                if (typeof onItemClick === 'function') {
                    onItemClick({ data: item, index, target: e.currentTarget });
                }
            };

            baseLine.appendChild(contentDiv);

            const eventWrap = document.createElement('div');
            eventWrap.className = `timeline-event ${positionClass}`;
            eventWrap.appendChild(circleLine);
            eventWrap.appendChild(baseLine);

            timelineItem.innerHTML = `<div class="timeline-date">${item.date}</div>`;
            timelineItem.appendChild(eventWrap);

            timelineEl.appendChild(timelineItem);
        });

        container.innerHTML = '';
        container.appendChild(timelineEl);

        function adjustTimelineHeight() {
            let maxHeight = 0;
            const contents = timelineEl.querySelectorAll('.timeline-vtc-li-base');
            contents.forEach(content => {
                maxHeight = Math.max(maxHeight, content.offsetHeight);
            });

            const lines = timelineEl.querySelectorAll('.timeline-vtc-line');
            lines.forEach(line => {
                line.style.height = maxHeight + 'px';
            });

            const buffer = 40;
            container.style.paddingTop = (maxHeight + buffer) + 'px';
            container.style.paddingBottom = (maxHeight + buffer) + 'px';

            const timelineWidth = timelineEl.offsetWidth;
            const style = document.querySelector('#dynamic-style') || document.createElement('style');
            style.id = 'dynamic-style';
            style.textContent = `
                .timeline-wrapper::before {
                    width: ${timelineWidth + 10}px;
                }
            `;
            if (!document.querySelector('#dynamic-style')) {
                document.head.appendChild(style);
            }
        }

        requestAnimationFrame(adjustTimelineHeight);
        window.addEventListener('resize', adjustTimelineHeight);
    }

    document.addEventListener('DOMContentLoaded', () => {
        const timelineData = [
            { id: '1', date: '2025-01-01', content: '发布第2版2' },
            { id: '2', date: '2025-02-02', content: '发布第2版' },
            { id: '3', date: '2025-02-02', content: '发布第2版发布第2版发布第2版本发布第2版本' },
            // 你可以继续添加更多数据
        ];

        initTimeline({
            containerId: 'timeline-container',
            data: timelineData,
            onItemClick: ({ data, index, target }) => {
                console.log('用户点击内容：', data);
                // 举例：点击后高亮
                // target.style.backgroundColor = '#ffefc2';
            }
        });
    });
</script>
</body>
</html>
